﻿@namespace BlazorComponent
@inherits BRoutableGroupItem<ItemGroupBase>

<CascadingValue Value="IsDark" Name="IsDark">
    <Element Class="@CssProvider.GetClass(addDefaultCssImplicitly: false)"
             Style="@CssProvider.GetStyle()"
             Tag="@Tag"
             ReferenceCaptureAction="_ => Ref = _"
             id="@Id"
             @onclick="HandleOnClick"
             __internal_stopPropagation_onclick="@OnClickStopPropagation"
             __internal_preventDefault_onclick="@OnClickPreventDefault"
             @attributes="@Attributes">
        @if (ItemContent != null)
        {
            @ItemContent?.Invoke(GenItemContext())
        }
        else
        {
            @GenAvatarCore(PrependAvatar)
            @GenIcon(PrependIcon)
            @GenContent()
            @GenIcon(AppendIcon)
            @GenAvatarCore(AppendAvatar)
            @ChildContent
        }
    </Element>
</CascadingValue>

@code {

    RenderFragment GenIcon(string? icon) => __builder =>
    {
        @if (!string.IsNullOrWhiteSpace(icon))
        {
            <BListItemIcon>
                <BIcon>@icon</BIcon>
            </BListItemIcon>
        }
    };

    private RenderFragment GenAvatarCore(string? src) => __builder =>
    {
        @if (!string.IsNullOrWhiteSpace(src))
        {
            @GenAvatar(src)
        }
    };

    protected virtual RenderFragment GenAvatar(string? src) => __builder => { };

    RenderFragment GenContent() => __builder =>
    {
        if (HasBuiltInContent)
        {
            <BListItemContent>
                <BListItemTitle>
                    @Title
                </BListItemTitle>
                <BListItemSubtitle>
                    @Subtitle
                </BListItemSubtitle>
            </BListItemContent>
        }
    };

}